/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javacroft.view.GUI;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javacroft.facade.Facade;
import javacroft.view.GUI.Enseignant;
import javacroft.view.MouseOverHintManager;
import javacroft.view.MyPanel;
import javacroft.view.ViewIF;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 *
 * @author julien
 */
public class Principale extends JFrame implements ViewIF, ActionListener, KeyListener
{

   Facade myControlleur;
   Toolkit tk;
   Dimension ecran;
   MouseOverHintManager myHintManager;
   //JFrame acueil = new JFrame("Accueil");
   MyPanel conteneur;
   JButton gestionNotes = new JButton("Attribution Notes");
   JButton consultEnseignements = new JButton("Cours");
   JButton juryAnnee = new JButton("Jury année");
   JButton jurySemestre = new JButton("Jury Semestre");
   JButton APDJ = new JButton("APDJ");
   JButton consultNotes = new JButton("Consultation notes");
   JButton connection = new JButton("Connexion");
   JFrame login = new JFrame("Connexion");
   JTextField jtfLogin = new JTextField();
   JTextField jtfPass = new JTextField();
   JButton validLog = new JButton("Valider");
   JButton cancelLog = new JButton("Quitter");
   Font police = new Font("Serif", 0, 20);
   Font policeLabel = new Font("Serif", 0, 15);
   Font policeBulle = new Font("Serif", 0, 15);
   JTextArea nomConnecte = new JTextArea();
   JTextArea labelBullePrincipal;
   MyPanel logo = new MyPanel("Polytech.png", 250, 35);

   public Principale(Facade unControlleur)
   {

      myControlleur = unControlleur;
      unControlleur.setView((ViewIF) this);

      tk = Toolkit.getDefaultToolkit();
      ecran = tk.getScreenSize();

      GridBagConstraints gbc = new GridBagConstraints();


      labelBullePrincipal = new JTextArea();
      labelBullePrincipal.setForeground(Color.black);
      labelBullePrincipal.setEditable(false);
      labelBullePrincipal.setOpaque(false);
      labelBullePrincipal.setFont(policeBulle);
      labelBullePrincipal.setFont(labelBullePrincipal.getFont().deriveFont(Font.BOLD));
      myHintManager = new MouseOverHintManager(new JLabel(), labelBullePrincipal);


      conteneur = new MyPanel("ecran8.jpg", 840, 500);
      conteneur.setLayout(new GridBagLayout());
      conteneur.setSize(new Dimension(840, 500));
      myHintManager.addHintFor(conteneur, "Choisissez l'action à réaliser. Pour plus de précision, placez la souris sur l'un des boutons.");

      JTextArea labelBienvenue = new JTextArea("Bonjour et bienvenue dans le module de gestion des notes des étudiants de Polytech'Montpellier !!!");
      labelBienvenue.setOpaque(false);
      labelBienvenue.setForeground(Color.BLUE);
      labelBienvenue.setLineWrap(true);
      labelBienvenue.setWrapStyleWord(true);
      labelBienvenue.setEditable(false);
      labelBienvenue.setPreferredSize(new Dimension(780, 60));

      myHintManager.addHintFor(consultEnseignements, "Consultation des structures d'enseignements");
      consultEnseignements.setPreferredSize(new Dimension(250, 100));
      consultEnseignements.setFont(police);
      consultEnseignements.addActionListener(this);
      consultEnseignements.setActionCommand("SE");
      consultEnseignements.setEnabled(false);

      myHintManager.addHintFor(gestionNotes, "Ajout et modification des notes pour les étudiants");
      gestionNotes.setPreferredSize(new Dimension(250, 100));
      gestionNotes.setFont(police);
      gestionNotes.addActionListener(this);
      gestionNotes.setActionCommand("notes");
      gestionNotes.setEnabled(false);

      myHintManager.addHintFor(APDJ, "Attribution des APDJ");
      APDJ.setPreferredSize(new Dimension(250, 100));
      APDJ.setFont(police);
      APDJ.addActionListener(this);
      APDJ.setActionCommand("APDJ");
      APDJ.setEnabled(false);

      myHintManager.addHintFor(juryAnnee, "Attribution des Points Jury Année");
      juryAnnee.setFont(police);
      juryAnnee.setPreferredSize(new Dimension(250, 100));
      juryAnnee.addActionListener(this);
      juryAnnee.setActionCommand("juryA");
      juryAnnee.setEnabled(false);

      myHintManager.addHintFor(jurySemestre, "Attribution des Points Jury Semestre");
      jurySemestre.setFont(police);
      jurySemestre.setPreferredSize(new Dimension(250, 100));
      jurySemestre.addActionListener(this);
      jurySemestre.setActionCommand("juryS");
      jurySemestre.setEnabled(false);

      myHintManager.addHintFor(connection, "Cliquez pour vous (dé)connecter.");
      connection.setFont(police);
      connection.setPreferredSize(new Dimension(180, 40));
      connection.addActionListener(this);
      connection.setActionCommand("Connection");

      nomConnecte.setForeground(Color.red);
      nomConnecte.setPreferredSize(new Dimension(250, 60));
      nomConnecte.setFont(policeLabel);
      nomConnecte.setText("Cliquez sur connexion pour acceder aux services qui vous concernent.");
      nomConnecte.setFont(nomConnecte.getFont().deriveFont(Font.BOLD));
      nomConnecte.setOpaque(false);
      nomConnecte.setLineWrap(true);
      nomConnecte.setWrapStyleWord(true);
      nomConnecte.setEditable(false);

      //*************FENETRE DE LOGIN**********************
      JPanel conteneurLog = new JPanel(new GridBagLayout());
      GridBagConstraints gbcLog = new GridBagConstraints();

      //**ajout du label et JTextField login
      JLabel labelLogin = new JLabel("Identifiant");
      gbcLog.insets = new Insets(20, 10, 0, 10);
      gbcLog.anchor = GridBagConstraints.CENTER;

      logo.setPreferredSize(new Dimension(250, 35));

      gbcLog.gridx = 0;
      gbcLog.gridy = 0;
      gbcLog.gridwidth = 9;
      conteneurLog.add(logo, gbcLog);

      gbcLog.insets = new Insets(10, 10, 0, 10);
      gbcLog.gridwidth = 2;
      gbcLog.gridx = 0;
      gbcLog.gridy = 1;
      gbcLog.anchor = GridBagConstraints.WEST;
      conteneurLog.add(labelLogin, gbcLog);

      gbcLog.gridy = 2;
      //gbcLog.anchor = GridBagConstraints.CENTER;
      jtfLogin.setPreferredSize(new Dimension(190, 20));
      jtfLogin.addKeyListener(this);
      conteneurLog.add(jtfLogin, gbcLog);

      //**ajout du Label et JTextField password
      JLabel labelPassword = new JLabel("Mot de passe");
      gbcLog.gridy = 3;
      conteneurLog.add(labelPassword, gbcLog);

      gbcLog.gridy = 4;
      jtfPass.setPreferredSize(new Dimension(190, 20));
      jtfPass.addKeyListener(this);
      conteneurLog.add(jtfPass, gbcLog);

      //bouton valider
      validLog.addActionListener(this);
      validLog.setActionCommand("Valider");
      gbcLog.gridy = 5;
      gbcLog.gridwidth = 1;
      gbcLog.gridx = 0;
      gbcLog.anchor = GridBagConstraints.EAST;
      conteneurLog.add(validLog, gbcLog);
      //bouton annuler
      gbcLog.gridx = 1;
      gbcLog.anchor = GridBagConstraints.WEST;
      cancelLog.addActionListener(this);
      cancelLog.setActionCommand("Annuler");
      conteneurLog.add(cancelLog, gbcLog);

      //fenetre de login
      login.setSize(new Dimension(300, 300));
      login.setLayout(new FlowLayout());
      login.add(conteneurLog);
      login.setResizable(false);
      login.setLocation(ecran.width / 2 - 100, ecran.height / 2 - 100);
      login.setVisible(true);



      gbc.insets = new Insets(10, 10, 0, 0);
      gbc.anchor = GridBagConstraints.WEST;
      gbc.gridwidth = 3;
      gbc.gridx = 0;
      gbc.gridy = 0;
      labelBienvenue.setFont(police);
      conteneur.add(labelBienvenue, gbc);

      gbc.gridwidth = 2;
      gbc.gridheight = 1;
      gbc.anchor = GridBagConstraints.EAST;

      gbc.gridx = 0;
      gbc.gridy = 1;
      conteneur.add(consultEnseignements, gbc);

      gbc.gridy = 2;
      conteneur.add(gestionNotes, gbc);

      gbc.gridy = 3;
      conteneur.add(APDJ, gbc);

      gbc.anchor = GridBagConstraints.CENTER;

      gbc.gridx = 1;
      gbc.gridy = 2;
      conteneur.add(connection, gbc);

      gbc.gridx = 2;
      gbc.gridy = 1;
      conteneur.add(jurySemestre, gbc);

      gbc.gridy = 2;
      conteneur.add(juryAnnee, gbc);

      gbc.gridy = 3;
      conteneur.add(nomConnecte, gbc);

      labelBullePrincipal.setPreferredSize(new Dimension(800, 50));
      gbc.gridwidth = 3;
      gbc.gridx = 0;
      gbc.gridy = 4;
      conteneur.add(labelBullePrincipal, gbc);

      myHintManager.enableHints(conteneur);
      this.setLocation(ecran.width / 2 - 405, ecran.height / 2 - 245);
      this.setSize(new Dimension(810, 490));
      this.setVisible(true);
      this.setLayout(new FlowLayout(0, 0, 0));
      this.add(conteneur);
      this.setResizable(false);
      this.setDefaultCloseOperation(EXIT_ON_CLOSE);
      jtfLogin.requestFocusInWindow();
      login.toFront();
      jtfLogin.requestFocus();
   }

   public void display(String message)
   {
      JOptionPane.showMessageDialog(this, message);
   }

   public void actionPerformed(ActionEvent e)
   {
      if (e.getActionCommand().equals("Valider"))
      {         
         login();
      }
      if (e.getActionCommand().equals("Annuler"))
      {
         login.setVisible(false);
      }
      else if (e.getActionCommand().equals("juryS"))
      {
         try
         {
            myControlleur.setAction("JuryS");
            JurySemestre JS = new JurySemestre(myControlleur, this);
            this.setVisible(false);
         }
         catch (Exception ex)
         {
            Logger.getLogger(Principale.class.getName()).log(Level.SEVERE, null, ex);
         }

      }
      else if (e.getActionCommand().equals("juryA"))
      {
         try
         {
            myControlleur.setAction("JuryA");
            JuryAnnee JA = new JuryAnnee(myControlleur, this);
            this.setVisible(false);
         }
         catch (Exception ex)
         {
            Logger.getLogger(Principale.class.getName()).log(Level.SEVERE, null, ex);
         }

      }
      if (e.getActionCommand().equals("notes"))
      {
         try
         {
            myControlleur.setAction("Enseignant");
            Enseignant notes = new Enseignant(myControlleur, this);
            this.setVisible(false);
         }
         catch (Exception ex)
         {
            Logger.getLogger(Principale.class.getName()).log(Level.SEVERE, null, ex);
         }

      }
      else if (e.getActionCommand().equals("SE"))
      {

         StructureEnseignement SE = new StructureEnseignement(myControlleur, this);
         this.setVisible(false);
      }
      else if (e.getActionCommand().equals("APDJ"))
      {
         try
         {
            myControlleur.setAction("RespUE");
            ResponsableUE RUE = new ResponsableUE(myControlleur, this);
            this.setVisible(false);
         }
         catch (Exception ex)
         {
            Logger.getLogger(Principale.class.getName()).log(Level.SEVERE, null, ex);
         }

      }
      else if (e.getActionCommand().equals("Connection"))
      {
         try
         {
            if (((JButton) e.getSource()).getText().equals("Connexion"))
            {
               login.setVisible(true);
               jtfLogin.setSelectionStart(0);
               jtfLogin.setSelectionEnd(jtfLogin.getText().length());
               jtfLogin.requestFocus();
            }
            else
            {
               nomConnecte.setText("Cliquez sur connexion pour acceder aux services qui vous concernent.");
            }
            jtfPass.setText("");
            juryAnnee.setEnabled(false);
            jurySemestre.setEnabled(false);
            APDJ.setEnabled(false);
            consultEnseignements.setEnabled(false);
            gestionNotes.setEnabled(false);
            myControlleur.deconnecter();
         }
         catch (Exception ex)
         {
            display("Erreur lors de la deconnexion");
            ex.printStackTrace();
         }
         ((JButton) e.getSource()).setText("Connexion");
      }

   }

   public void keyPressed(KeyEvent e)
   {
      if (e.getKeyCode() == KeyEvent.VK_ENTER)
      {
         if (jtfLogin.hasFocus())
         {
            jtfPass.requestFocus();
         }
         else if (jtfPass.hasFocus())
         {
            login();
         }
      }
   }

   public void keyReleased(KeyEvent arg0)
   {
      
   }

   public void keyTyped(KeyEvent arg0)
   {
   }

   public void login()
   {
      if (jtfLogin.getText().equals("") || jtfPass.getText().equals(""))
      {
         display("Vous devez renseigner tous les champs !");
      }
      else
      {
         try
         {
            boolean isLogger = myControlleur.verifLogin(jtfLogin.getText(), jtfPass.getText());
            if (!isLogger)
            {
               display("Vos identifiants n'ont pas été reconnus. Veuillez recommencer.");
               jtfLogin.requestFocus();
               jtfLogin.setSelectionStart(0);
               jtfLogin.setSelectionEnd(jtfLogin.getText().length());
               jtfLogin.requestFocus();
               jtfPass.setText("");
            }
            else
            {
               login.setVisible(false);
               nomConnecte.setText("Bonjour " + jtfLogin.getText());
               connection.setText("Deconnexion");
               consultEnseignements.setEnabled(true);
               //if(myControlleur.estResponsableECUE())
               gestionNotes.setEnabled(true);
               //if(myControlleur.estResponsableUE())
               APDJ.setEnabled(true);
               //if(myControlleur.estResponsableAnnee() || myControlleur.estResponsableDepartement())
               //{
               jurySemestre.setEnabled(true);
               juryAnnee.setEnabled(true);
               //}
            }
         }
         catch (Exception ex)
         {           
            display("Erreur lors de la vérification du login !");
            ex.printStackTrace();
         }
      }
   }
}
